#include <U8g2lib.h>
#include <Wire.h>
// 自定义模块
#include "OledModule/Oled.h"


int startX = 60; // 屏幕中心X位置
int startY = 25; // 屏幕中心Y位置
int eyeRadius = 8; // 眼睛的半径
int eyeOffset = 15; // 左右眼之间的距离
int verticalOffset = 2; // 垂直平移距离

// 函数声明
void drawEye(int x, int y, int radius, int direction, bool isClosed = false);

void initEye() {
  randomSeed(analogRead(0)); // 随机种子
}

void drawRadomEye() {
  u8g2.clearBuffer(); // 清除屏幕缓存

  // 计算左右眼睛的X位置
  int leftEyeX = startX - eyeOffset;
  int rightEyeX = startX + eyeOffset;

  // 计算垂直平移
  int verticalShift = random(-verticalOffset, verticalOffset + 1);

  // 随机选择眼睛状态
  int eyeState = random(0, 4); // 0:睁眼, 1:闭眼, 2:左看, 3:右看

  // 根据眼睛状态进行绘制
  if (eyeState == 0 || eyeState == 2 || eyeState == 3) {
    int leftEyeRadius = (eyeState == 2) ? eyeRadius -1 : eyeRadius; // 左看时左眼变成1/3大小
    int rightEyeRadius = (eyeState == 3) ? eyeRadius -1 : eyeRadius; // 右看时右眼变成1/3大小
    int direction = (eyeState == 2) ? -2 : (eyeState == 3) ? 2 : 0; // 左看:-2, 右看:+2, 正看:0
    drawEye(leftEyeX, startY + verticalShift, leftEyeRadius, direction);
    drawEye(rightEyeX, startY + verticalShift, rightEyeRadius, direction);
  }
  else if (eyeState == 1) {
    drawEye(leftEyeX, startY, eyeRadius, 0, true);
    drawEye(rightEyeX, startY, eyeRadius, 0, true);
  }

  u8g2.sendBuffer(); // 发送缓存到屏幕

  delay(600); // 每次更新间隔0.6秒
}

// 画眼睛的函数定义
void drawEye(int x, int y, int radius, int direction, bool isClosed) {
  if (!isClosed) {
    // 画实心圆作为眼睛
    u8g2.drawDisc(x + direction, y, radius, U8G2_DRAW_ALL);
  } else {
    // 闭眼状态：不画圆，直接返回
  }
}